#!/bin/sh

#
# Test out progress reporting on a firmware write
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"

cat >$CONFIG <<EOF
file-resource 1.bin {
	host-path = "${TESTFILE_1K}"
}
file-resource 2.bin {
	host-path = "${TESTFILE_1K}"
}
file-resource 3.bin {
	host-path = "${TESTFILE_1K}"
}
file-resource 4.bin {
	host-path = "${TESTFILE_1K}"
}

task complete {
	on-resource 1.bin { raw_write(0) }
	on-resource 2.bin { raw_write(0) }
	on-resource 3.bin { raw_write(0) }
	on-resource 4.bin { raw_write(0) }
}
EOF

$FWUP_CREATE -c -f $CONFIG -o $FWFILE

# Run the command and turn the `\r`s into `\n`'s to make it easy to compare
# with the expected output. The first call to sed trims the byte counts which
# can vary slightly depending on libarchive versions in use. The second call to
# sed is to discard the final empty line that's present on Windows.
$FWUP_APPLY -a -d $IMGFILE -i $FWFILE -t complete \
    | tr '\r' '\n' \
    | sed 's/].*$/]/g' \
    | sed '/^$/d' \
    | grep -v '^Elapsed time' \
    > $WORK/actual_output.txt

cat >$WORK/expected_output.txt <<EOF
  0% [                                    ]
 25% [=========                           ]
 50% [==================                  ]
 75% [===========================         ]
 99% [=================================== ]
100% [====================================]
Success!
EOF
diff $WORK/expected_output.txt $WORK/actual_output.txt

# Check that the verify logic works on this file
$FWUP_VERIFY -V -i $FWFILE

